package http;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Subscriber;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 改造助手
 *
 * @author zhangtao
 * @date 2021/11/18
 */
@Slf4j
public class RetrofitHelper {
    private static final String APP_URL = "https://api.apiopen.top/";
    private static RetrofitHelper instance = null;
    private GsonConverterFactory factory = GsonConverterFactory.create(new GsonBuilder().create());
    private Retrofit appRetrofit = null;

    public static RetrofitHelper getInstance() {
        if (null == instance) {
            instance = new RetrofitHelper();
        }
        return instance;
    }

    public RetrofitHelper() {
        initAppRetrofit();
    }

    /**
     * 初始化AppRetrofit
     */
    private void initAppRetrofit() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(40, TimeUnit.SECONDS)
                .readTimeout(40, TimeUnit.SECONDS).writeTimeout(40, TimeUnit.SECONDS).build();
        appRetrofit = new Retrofit.Builder()
                .baseUrl(APP_URL)
                .client(okHttpClient)
                .addConverterFactory(factory)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
    }

    public ApiService getService() {
        return appRetrofit.create(ApiService.class);
    }

    public static void main(String[] args) {
        sync();
        async();
        rxsync();
    }


    public static void sync() {
        try {
            ApiService apiService = RetrofitHelper.getInstance().getService();
            Call<ApiData> call = apiService.getJokeCall(1, 1, "video");
            Response<ApiData> response = call.execute();
            if (response.isSuccessful()) {
                ApiData result = response.body();
                if (null != result) {
                    log.info("{} sync:{}", Thread.currentThread().getName(), new Gson().toJson(result));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void async() {
        try {
            ApiService apiService = RetrofitHelper.getInstance().getService();
            Call<ApiData> call = apiService.getJokeCall(1, 1, "video");
            call.enqueue(new Callback<ApiData>() {
                @Override
                public void onResponse(Call<ApiData> call, Response<ApiData> response) {
                    if (response.isSuccessful()) {
                        ApiData result = response.body();
                        if (null != result) {
                            log.info("{} async:{}", Thread.currentThread().getName(), new Gson().toJson(result));
                        }
                    }
                }

                @Override
                public void onFailure(Call<ApiData> call, Throwable t) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void rxsync() {
        try {
            ApiService apiService = RetrofitHelper.getInstance().getService();
            apiService.getJokeObservable(1, 1, "video")
                    .subscribe(new Subscriber<Response<ApiData>>() {
                        @Override
                        public void onCompleted() {
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onNext(Response<ApiData> response) {
                            ApiData result = response.body();
                            if (null != result) {
                                log.info("{} rxsync:{}", Thread.currentThread().getName(), new Gson().toJson(result));
                            }
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
